package com.hp.attendance.service;

import com.hp.attendance.dao.EmployeeBysDao;
import com.hp.attendance.dao.EmployeeDao;
import com.hp.attendance.entity.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EmployeeBysService {
    private EmployeeDao employeeDao = new EmployeeDao();
    private EmployeeBysDao employeeBysDao = new EmployeeBysDao();

    //查询所有员工信息
    public List<Employee> getEmpLista() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        List<Employee> list = employeeBysDao.getEmpList();
        return list;
    }

    //根据部门编号查询部门名称
    public Department getDepName(int department_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Department department = employeeBysDao.getDepName(department_number);
        return department;
    }

    //根据职位编号查询职位名称
    public Position getPosName(int position_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Position position = employeeBysDao.getPosName(position_number);
        return position;
    }


    /**
     * 分页查询员工列表
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public PageInfo<EmployeeBys> getPage(int pageNo , int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        PageInfo<EmployeeBys> page = new PageInfo<EmployeeBys>();
        //查询分页的数据                           2       10
        List<Employee> list = employeeDao.getLimitList(pageNo, pageSize);

        ArrayList<EmployeeBys> employeeBys = new ArrayList<EmployeeBys>();


        for (Employee employee : list) {
            Integer employee_number = employee.getEmployee_number();
            EmployeeBys emp = new EmployeeBys(employee.getId(),employee.getEmployee_number(),employee.getName(),employee.getGender(),
                    employee.getBirthday(),employee.getTelephone(),employee.getEmail(),employee.getAddress(),employee.getEducation(),
                    getDepName(employee.getDepartment_number()).getName(),getPosName(employee.getPosition_number()).getName(),
                    employee.getIn_time(),employee.getPassword(),employee.getNotes());
            employeeBys.add(emp);
        }

        page.setRecords(employeeBys);
        //查询总记录条数
        long total = employeeBysDao.getTotal();
        page.setTotal(total);
        //设置当前页码
        page.setCurrent(pageNo);
        //设置总页数
        int pages = (int)Math.ceil(total / (pageSize * 1.0));
        page.setPages(pages);
        return page;
    }

    /**
     * 添加员工
     * @param employee
     * @throws SQLException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void AddEmployee(Employee employee) throws SQLException, IOException, ClassNotFoundException {
        employeeBysDao.AddEmployee(employee);
    }


    /**
     * 获取部门信息
     * @return
     */
    public List<Department> getDepartment() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        List<Department> list = employeeBysDao.getDepartment();
        return list;
    }

    /**
     * 获取职称信息
     * @return
     */
    public List<Position> getPosition() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        List<Position> list = employeeBysDao.getPosition();
        return list;
    }


    /**
     * 根据工号查询employee信息
     * @param employee_number
     * @return
     */
    public Employee getEmployeeByNum(int employee_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Employee employee = employeeBysDao.getEmployeeByNum(employee_number);
        return employee;
    }


    /**
     * 修改员工信息
     * @param employee
     */
    public void UpdateEmployeeByNum(Employee employee) throws SQLException, IOException, ClassNotFoundException {
        employeeBysDao.UpdateEmployeeByNum(employee);
    }
    /**
     * 修改在职状态
     * @param status
     * @param employee_number
     * @throws SQLException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void UpdateHistoryByNum(String status,int employee_number) throws SQLException, IOException, ClassNotFoundException {
        employeeBysDao.UpdateHistoryByNum(status,employee_number);
    }


    /**
     * 根据员工编号删除员工信息
     * @param employee_number
     */
    public void DelEmployee(int employee_number) throws SQLException, IOException, ClassNotFoundException {
        employeeBysDao.DelEmployee(employee_number);
    }
    /**
     *  修改离职或退休时间
     * @param date
     * @param employee_number
     * @throws SQLException
     * @throws IOException
     * @throws ClassNotFoundException
     */


    public void UpdateOut_timeByNum(Date date, int employee_number) throws SQLException, IOException, ClassNotFoundException {
       employeeBysDao.UpdateOut_timeByNum(date,employee_number);
    }
    }
